//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.PssProductCodeDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.dao.wills.PssPurcReceiveDao;
import com.aidong.dao.wills.PssPurcReceiveListDao;
import com.aidong.model.wills.Products;
import com.aidong.model.wills.PssPopProduct;
import com.aidong.model.wills.PssProductCodeAndProvider;
import com.aidong.model.wills.PssPurcReceive;
import com.aidong.model.wills.PssPurcReceiveImpl;
import com.aidong.model.wills.PssPurcReceiveList;
import com.aidong.service.PssPrintDataService;
import com.aidong.util.ProductUtil;
import com.aidong.web.controller.pss.PopProductController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"purcReceive"})
public class PurcReceiveController extends PopProductController {
    @Resource
    PssPurcReceiveDao pssPurcReceiveDao;
    @Resource
    PssPurcReceiveListDao pssPurcReceiveListDao;
    @Resource
    PssProductStockDao pssProductStockDao;
    @Resource
    PssProductCodeDao pssProductCodeDao;
    @Resource
    ProductUtil productUtil;
    @Resource
    PssPrintDataService pssprintDataService;

    public PurcReceiveController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("pss/purcreceive/list");
        return mv;
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "oaOrderId",required = false) String oaOrderId, @RequestParam(value = "status",required = false) String status, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "shopCode", shopCode);
        cd.eq("oaOrderId", oaOrderId);
        cd.eq("Status", status);
        this.pssPurcReceiveDao.queryPageList(pg, cd, (new OrderSet()).asc("oaOrderId"));
        ModelAndView mv = new ModelAndView("pss/purcreceive/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam("oaOrderId") String oaOrderId) {
        PssPurcReceiveImpl pssPurcReceiveImpl = new PssPurcReceiveImpl();
        pssPurcReceiveImpl.setOaOrderId(oaOrderId);
        ModelAndView mv = new ModelAndView("pss/purcreceive/edit");
        mv.addObject("pssPurcReceive", pssPurcReceiveImpl);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("pssPurcReceive") PssPurcReceive pssPurcReceive, Products products) {
        if(this.chkNull(pssPurcReceive.getOaOrderId())) {
            return this.getResult(false, "请输入OA订单号");
        } else {
            try {
                pssPurcReceive.setReceiveDate(new Date());
                pssPurcReceive.setShopCode(this.getLoginShopCode());
                pssPurcReceive.setStatus(Integer.valueOf(0));
                pssPurcReceive.setPrintLabel("N");
                pssPurcReceive.setCreateuser(this.getLoginUserCode());
                pssPurcReceive.setCreatetime(new Date());
                pssPurcReceive.setUpdatetime(new Date());
                PssPurcReceiveList e = null;
                PssProductCodeAndProvider product = null;
                ArrayList lst = new ArrayList();
                Iterator i$ = products.getLstProduct().iterator();

                while(i$.hasNext()) {
                    PssPopProduct pc = (PssPopProduct)i$.next();
                    e = new PssPurcReceiveList();
                    if(!this.chkNull(pc.getProductCode())) {
                        product = this.productUtil.getProductInfo(pc.getProductCode());
                        e.setProductCode(pc.getProductCode());
                        e.setPrice(product.getPurcPrice());
                        e.setProviderCode(product.getProviderCode());
                        e.setQuantity(pc.getQty());
                        lst.add(e);
                    }
                }

                this.pssPurcReceiveDao.addPurcReceive(pssPurcReceive, this.pssPurcReceiveListDao, this.pssProductStockDao, lst);
                this.addUserLog("新增采购入库的商品", "入库单号:" + pssPurcReceive.getReceiveID() + ",OA订单号:" + pssPurcReceive.getOaOrderId());
                return this.getResult(true, "新增采购入库的商品完成");
            } catch (Exception var8) {
                this.log.error("add purcReceive", var8);
                return this.getResult(false, "新增采购入库的商品发生错误");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("receiveID") Integer receiveID) {
        PssPurcReceive pssPurcReceive = (PssPurcReceive)this.pssPurcReceiveDao.getById(receiveID);
        if(pssPurcReceive == null) {
            return this.showAlert("没有这个入库单号的商品");
        } else if(pssPurcReceive.getStatus().intValue() == 1) {
            return this.showAlert("入库单已经入库完了，不能修改入库数");
        } else if("Y".equals(pssPurcReceive.getPrintLabel())) {
            return this.showAlert("入库单已经打印完成，不能修改入库数");
        } else {
            ConditionMap cd = new ConditionMap();
            cd.eq("ReceiveID", receiveID);
            List lst = this.pssPurcReceiveListDao.queryPurcReceiveRptList(cd);
            ModelAndView mv = new ModelAndView("pss/purcreceive/edit");
            mv.addObject("pssPurcReceive", pssPurcReceive);
            mv.addObject("list", lst);
            mv.addObject("productUnit", this.getTypeList("productUnit"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("pssPurcReceive") PssPurcReceive pssPurcReceive, Products products) {
        try {
            this.pssPurcReceiveDao.updatePurcReceive(this.pssPurcReceiveListDao, this.pssProductStockDao, pssPurcReceive.getShopCode(), products);
            this.addUserLog("修改采购入库的商品", "入库单号:" + pssPurcReceive.getReceiveID() + ",OA订单号:" + pssPurcReceive.getOaOrderId());
        } catch (Exception var4) {
            this.log.error("edit purcReceive", var4);
            return this.getResult(false, "修改采购入库的商品发生错误");
        }

        return this.getResult(true, "修改采购入库的商品完成");
    }

    @RequestMapping({"_confirm"})
    @ResponseBody
    public String _confirm(@RequestParam("receiveID") Integer receiveID, @RequestParam(value = "shopCode",required = false) String shopCode) {
        try {
            PssPurcReceive e = (PssPurcReceive)this.pssPurcReceiveDao.getById(receiveID);
            if(e == null) {
                return this.getResult(false, "没有这个入库单号的商品");
            }

            if(e.getStatus().intValue() == 1) {
                return this.getResult(false, "入库单已经入库完了，不能再入库");
            }

            this.pssPurcReceiveDao.updatePurcReceiveForConfirm(this.pssPurcReceiveListDao, this.pssProductStockDao, receiveID, shopCode, this.getLoginUserCode());
            this.addUserLog("采购入库的商品入库完了", "入库单号:" + e.getReceiveID() + ",OA订单号:" + e.getOaOrderId());
        } catch (Exception var4) {
            this.log.error("edit purcReceive", var4);
            return this.getResult(false, "采购入库的商品入库时发生错误");
        }

        return this.getResult(true, "采购入库的商品入库完了");
    }

    @RequestMapping({"print"})
    public ModelAndView print(@RequestParam("receiveID") Integer receiveID) {
        StringBuilder msg = new StringBuilder();
        String data = this.pssprintDataService.getPssReceiveData(receiveID, msg);
        if(data == null) {
            return this.showAlert(msg.toString());
        } else {
            ModelAndView mv = new ModelAndView("pss/print/print");
            mv.addObject("data", data);
            mv.addObject("title", "采购入库单打印");
            mv.addObject("ptjs", "pssreceive");
            return mv;
        }
    }
}
